home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17151 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  70 lines

  1. Path: nntp1.best.com!usenet
  2. From: javaprog@best.com (John Lockwood)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: How to copy text to Win95 clipboard?
  5. Date: Sun, 14 Apr 1996 01:40:37 GMT
  6. Organization: Best Internet Communications
  7. Message-ID: <4kpl36$j8c@nntp1.best.com>
  8. References: <316ff634.6913883@news.demon.co.uk>
  9. NNTP-Posting-Host: javaprog.vip.best.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. martin@mckean.demon.co.uk (Martin McKean) wrote:
  13.  
  14. >How do I set up a handle to a character string so that I can put that
  15. >string on the clipboard?
  16.  
  17. >I'm using VC4++; grateful for any help!!
  18.  
  19. Dude, here's a one-shot answer.  But I really urge you to get a copy
  20. of Charles Petzold's Programming Windows and read it.
  21.  
  22. #include <windows.h>
  23. #include <stdio.h>
  24. #include <assert.h>
  25.  
  26. int WINAPI WinMain(HINSTANCE  hInstance, HINSTANCE  hPrevInstance,
  27. LPSTR lpCmdLine, int  nShowCmd)
  28. {
  29. // Get a string and a length for the string:
  30. char * szTrySomething = "Thanks, John, for this free Windows lesson!"
  31. "  I promise not to send you E-Mail.";
  32. DWORD dwBytes = strlen(szTrySomething) + 1;
  33.     
  34. // Get a memory handle for the call
  35. HGLOBAL hGlobal = GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE,
  36. dwBytes);
  37. assert(hGlobal);
  38.     
  39. // Turn the memory handle into a string:
  40.  
  41. // Lock handle to get a pointer.
  42. char * szBuf = (char *) GlobalLock(hGlobal);
  43. assert(szBuf);
  44.  
  45. // Copy the string into the pointer associated with the handle
  46. strcpy(szBuf, szTrySomething);
  47.  
  48. // Unlock the pointer
  49. GlobalUnlock(hGlobal);
  50.  
  51. // Do the deed!
  52. OpenClipboard(NULL);        
  53. SetClipboardData(CF_TEXT, hGlobal);
  54. CloseClipboard();
  55.     
  56. return 0;
  57. }
  58.  
  59.  
  60.  
  61. Regards,
  62.  
  63.  
  64.  
  65. John Lockwood
  66. john@wwg.com
  67. javaprog@best.com
  68. http://www.best.com/~javaprog
  69.  
  70.